﻿using System;
using System.IO;
using System.Web;
using Google.GData.Client;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.YouTube;
using Social.YouTube;

namespace Social.YouTube
{
	public class YouTubeApi
	{
		private IYouTubeConfig config;

		public YouTubeApi(IYouTubeConfig config)
		{
			this.config = config;
		}

		public bool IsAuthenticated
		{
			get { return !string.IsNullOrEmpty(config.SessionToken); }
		}

		public Video UploadVideo(FileInfo file, string title, string description, string category, string keywords)
		{
			var request = new YouTubeRequest(new YouTubeRequestSettings(config.AppName, "", config.DeveloperKey, config.SessionToken));
			var newVideo = new Video {Title = title, Keywords = keywords, Description = description};

			newVideo.YouTubeEntry.Private = true;
			newVideo.Tags.Add(new MediaCategory(category, YouTubeNameTable.CategorySchema));
			//newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));

			//newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
			// alternatively, you could just specify a descriptive string
			// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");

			using (var fileStream = file.OpenRead())
			{
				newVideo.YouTubeEntry.MediaSource = new MediaFileSource(fileStream, file.Name, "application/octet-stream");
				request.Settings.Timeout = 120000;
				return request.Upload(newVideo);
			}
		}

		public string ConnectUrl
		{
			get
			{
				//if (string.IsNullOrEmpty(config.HostedDomain))
					return AuthSubUtil.getRequestUrl(config.CallbackUrl, "http://gdata.youtube.com", false, true);
				//return AuthSubUtil.getRequestUrl(config.HostedDomain, config.CallbackUrl, "http://gdata.youtube.com", false, true);
			}
		}

		public void Login()
		{
			var url = AuthSubUtil.getRequestUrl(config.CallbackUrl, "http://gdata.youtube.com", false, true);
			HttpContext.Current.Response.Redirect(url);
		}

		public void Authenticate()
		{
			var token = HttpContext.Current.Request.Params["token"];
			if (string.IsNullOrEmpty(token))
				return;
			config.SessionToken = AuthSubUtil.exchangeForSessionToken(token, null);
			config.SaveSessionToken();
		}

		/// <summary>
		/// gets a form upload token to enable form uploading
		/// </summary>
		/// <param name="title"></param>
		/// <param name="category"></param>
		/// <param name="keywords"></param>
		/// <param name="description"></param>
		/// <returns></returns>
		public FormUploadToken GetFormUploadToken(string title, string category, string keywords, string description)
		{
			var request = new YouTubeRequest(new YouTubeRequestSettings(config.AppName, "", config.DeveloperKey, config.SessionToken));
			Video newVideo = new Video();
			newVideo.Title = title;
			newVideo.Tags.Add(new MediaCategory(category, YouTubeNameTable.CategorySchema));
			newVideo.Keywords = keywords;
			newVideo.Description = description;
			newVideo.YouTubeEntry.Private = false;
			//newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag",
			// YouTubeNameTable.DeveloperTagSchema));
			//newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
			//newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");

			FormUploadToken token = request.CreateFormUploadToken(newVideo);
			return token;
		}
	}
}